package cn.dnuihyy406.dnuifood_16110100406.controller.mefragment;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import cn.dnuihyy406.dnuifood_16110100406.R;
import cn.dnuihyy406.dnuifood_16110100406.bean.GetCommentBean;
import cn.dnuihyy406.dnuifood_16110100406.bean.ResultBean;
import cn.dnuihyy406.dnuifood_16110100406.listener.OnRetrofitListener;
import cn.dnuihyy406.dnuifood_16110100406.model.UserImformationModel;

public class MyCommentAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>implements OnRetrofitListener<Object> {
    private Context mContext;
    private List<GetCommentBean>getCommentBeanList1;
    private LayoutInflater mLayoutInflatr;
    String Itemid;
    int p;

    public MyCommentAdapter (Context mContext,List getCommentBeanList1) {
        this.mContext=mContext;
        this.getCommentBeanList1=getCommentBeanList1;
        mLayoutInflatr=LayoutInflater.from(mContext);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View v=mLayoutInflatr.from(viewGroup.getContext()).inflate(R.layout.mycomment_item,viewGroup,false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder Holder, final int i) {
        final GetCommentBean x=getCommentBeanList1.get(i);
        if (null==x){
            return;
        }
        ViewHolder viewHolder=(ViewHolder)Holder;
        viewHolder.co_commenttime.setText(x.getComment_time());
        viewHolder.co_foodname.setText(x.getFoodname());
        viewHolder.co_price.setText(x.getContent());
        viewHolder.co_shopname.setText(x.getShopname());
        viewHolder.co_change.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(mContext,CommentActivity.class);
                String str = String.valueOf(getCommentBeanList1.get(i).getItem_id());
                intent.putExtra("itemid",str);
                mContext.startActivity(intent);
            }
        });
        Itemid=x.getItem_id();
        viewHolder.co_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                p=i;
                deleteComment();
            }
        });
    }

    @Override
    public int getItemCount() {
        return getCommentBeanList1.size();
    }

    @Override
    public void onSuccess(Object object, int flag) {
        if(((ResultBean)object).getSuccess().equals("1")){
            removeData(p);
            Toast.makeText(mContext,"删除成功",Toast.LENGTH_SHORT).show();
        }
        else
        {
            Toast.makeText(mContext,"删除失败",Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onFailure(String msg) {
        Toast.makeText(mContext,msg,Toast.LENGTH_SHORT).show();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView co_foodname,co_price,co_shopname,co_commenttime;
        Button co_change,co_delete;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            co_foodname = itemView.findViewById(R.id.co_foodname);
            co_shopname = itemView.findViewById(R.id.co_shopname);
            co_price = itemView.findViewById(R.id.co_price);
            co_commenttime=itemView.findViewById(R.id.co_commenttime);
            co_delete=itemView.findViewById(R.id.co_delete);
            co_change=itemView.findViewById(R.id.co_change);
        }
    }

    private void deleteComment(){
        UserImformationModel userImformationModel=new UserImformationModel();
        userImformationModel.deleteComment(Itemid,this);
    }

    public void removeData(int position){
        getCommentBeanList1.remove(position);
        notifyDataSetChanged();
    }
}